/*
 * G2P_Tone.cpp
 *
 *  Created on: Nov 30, 2012
 *      Author: bannok
 */
#include "g2p_tone.h"
bool tone_isToneSymbol(char * key, int b, int len) {
	if (b + 2 < len) {
		if (key[b + 1] == (signed char) 0xb9
				&& (key[b + 2] >= (signed char) 0x88
						&& key[b + 2] <= (signed char) 0x8b)) {
			return true;
		}
	}
	return false;
}
int tone_isFrontVowel(char * key, int b, int len) {
	if (b + 2 < len) {
		if (key[b + 1] == (signed char) 0xb9
				&& (key[b + 2] >= (signed char) 0x80
						&& key[b + 2] <= (signed char) 0x84)) {
			return key[b + 2] - (signed char) 0x80;
		}
	}
	return -1;
}
int tone_isBackVowel(char * key, int b, int len) {
	printf("\n%s b=%d c=%d len=%d",key+b,b,key[b+2],len);
	if (b + 2 < len) {
		if (key[b + 1] == (signed char) 0xb8
				&& (key[b + 2] >= (signed char) 0xb0
						&& key[b + 2] <= (signed char) 0xba)) {
			printf("\nhaha!");
			return key[b + 2] - (signed char) 0xb0;
		} else if (key[b + 1] == (signed char) 0xb9
				&& key[b + 2] <= (signed char) 0x87) {
			return 16;
		}
	}
	return -1;
}
int tone_toneSymbol(char * key, int b, int len) {
	if (b + 2 < len) {
		if (key[b + 1] == (signed char) 0xb9) {
			if (key[b + 2] == (signed char) 0x88)
				return 1;
			if (key[b + 2] == (signed char) 0x89)
				return 2;
			if (key[b + 2] == (signed char) 0x8a)
				return 3;
			if (key[b + 2] == (signed char) 0x8b)
				return 4;
		}
	}
	return 0;
}
bool tone_isCharSymbol(char * key, int b, int len) {
	if (b + 2 < len) {
		if (key[b + 1] == (signed char) 0xb8
				&& (key[b + 2] >= (signed char) 0x81
						&& key[b + 2] <= (signed char) 0xae)) {
			return true;
		}
	}
	return false;
}
bool tone_isVowelAfterSymbol(char * key, int b, int len) {
	if (b + 2 < len) {
		if (key[b + 1] == (signed char) 0xb8
				&& (key[b + 2] >= (signed char) 0xb1
						&& key[b + 2] <= (signed char) 0xb9)) {
			return true;
		}
	}
	return false;
}
void tone_ConsonantGroup(char * src, int b, int len) {
	if (b + 2 < len) {

	}
}
void tone_ConsonantToneMarker(char * text, int len, pronun_struct_unit * unit) {
	if (unit->sound == 1) { //find tone marker
		int tone = 0;
		for (int i = 0; i < len - 2; i = i + 3) {
			if (tone_isToneSymbol(text, i, len)) {
				tone = tone_toneSymbol(text, i, len);
			}
		}
		unit->t1 = tone;
	} else if (unit->sound == 2) {
		int conso = 0;
		int tone1 = 0;
		int tone2 = 0;
		for (int i = 0; i < len - 2; i = i + 3) {
			if (tone_isCharSymbol(text, i, len)) {
				conso++;
			}
			if (tone_isToneSymbol(text, i, len) && conso == 1) {
				tone1 = tone_toneSymbol(text, i, len);
			} else if (tone_isToneSymbol(text, i, len) && conso > 1) {
				tone2 = tone_toneSymbol(text, i, len);
				break;
			}
		}
		unit->t1 = tone1;
		unit->t2 = tone2;
	}
}
int tone_syllable_wordtype(int num) {
	int type = 0;
	bool lv = false;
	bool lw = false;
	int ic, v, fc;
	int tnum = num;
	int r;
	int s;
	r = num / 100;
	s = num % 100;
	fc = s;

	s = r % 100;
	r = r / 100;
	v = s;

	ic = r % 100;

	if(fc==3 || fc==4|| fc==5 || fc==6 || fc==7 || fc==8){
		if(fc==8 && v%2==0)
			return TONE_KAM_TAI_SHORT_SOUND;
		return TONE_KAM_PEN;
	}else{
		if (v % 2 == 1){
			return TONE_KAM_TAI_LONG_SOUND;
		}else{
			return TONE_KAM_TAI_SHORT_SOUND;
		}
	}
/*
	if (v % 2 == 1)
		lv = true;
	if (fc >= 3 && fc <= 8) {
		lw = true;
	}

	if (lv) { //long sound
		if (lw)
			type = 0;
		else
			type = 1;
	} else { //short sound
		if (v == 0 && (fc == 4 || fc == 6 || fc == 7)) {
			type = 0;
		} else {
			type = 2;
		}
	}*/
	return type;
}
void tone_syllable_sound_type(pronun_struct_unit * st) {
	int type = 0;
	if (st->sound == 1) {
		int type = tone_syllable_wordtype(st->st1);
		st->wt1 = type;
		//printf("\n->(%d) %d ", type, st->st1);
	} else if (st->sound == 2) {
		int type1 = tone_syllable_wordtype(st->st1);
		int type2 = tone_syllable_wordtype(st->st2);
		st->wt1 = type1;
		st->wt2 = type2;
		//printf("\n->(%d) %d ", type1, st->st1);
		//printf("\n->(%d) %d ", type2, st->st2);
	}
}
int tone_soundTone(pronun_struct_unit * st,int sound) {
	//printf("\nTone :%d %d %d",st->wt1,st->t1,st->c1);
	int word_type = st->wt1;
	int tone_maker = st->t1;
	int init_conso = st->c1;

	if(sound==2){
		 word_type = st->wt2;
		 tone_maker = st->t2;
		 init_conso = st->c2;
	}
	if(tone_maker==3){
		return TONE_FALLING;
	}else if(tone_maker==4){
		return TONE_RISING;
	}
	if (word_type == TONE_KAM_PEN) { //l
		if (tone_maker == TONE_BLANK) {
			switch (init_conso) {
			case 1:
				return TONE_RISING;
				break;
			case 2:
				return TONE_MID;
				break;
			case 3:
				return TONE_MID;
				break;
			case 4:
				return TONE_FALLING;
				break;
			default:
				return TONE_UNKNOWN;
			}
		} else if (tone_maker == TONE_MAI_EK) {
			return TONE_HIGH;
		} else if (tone_maker== TONE_MAI_THO) {
			if (init_conso == 1) {
				return TONE_LOW;
			} else if (init_conso > 1 && init_conso <= 4) {
				return TONE_MID_FALLING;
			}
		}
	} else if (word_type ==TONE_KAM_TAI_LONG_SOUND) { //dl
		if (init_conso>= 1 && init_conso <= 3) {
			return TONE_LOW;
		} else if (init_conso == 4) {
			return TONE_MID_FALLING;
		} else {
			return TONE_UNKNOWN;
		}
	} else if (word_type == TONE_KAM_TAI_SHORT_SOUND) { //ds
		if (init_conso >= 1 && init_conso <= 3) {
			return TONE_RISING;
		} else if (init_conso == 4) {
			return TONE_MID;
		} else {
			return TONE_UNKNOWN;
		}
	}
}
